package cn.elead.chaos.framework.web.enums;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import cn.elead.chaos.framework.web.endpoint.EpomEndPoint;
import cn.hutool.core.util.StrUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import lombok.Getter;
import lombok.Setter;

/**
 * <p>
 * 枚举信息相关接口
 * </p>
 *
 * @author luopeng
 */
@RestController
@Api(tags = { "EnumEndpoint" }, description = "枚举端点相关接口")
public class EnumEndpoint implements EpomEndPoint{

	private Environment environment;

	@Autowired
	public EnumEndpoint(Environment environment) {
		this.environment = environment;
	}

	/**
	 * 查询所有枚举信息
	 */
	@SuppressWarnings("rawtypes")
	@ApiOperation("查询枚举信息")
	@GetMapping("/web/enum/custom")
	public Map<String, List<Map<String, String>>> findCustomEnums() {

		Map<String, List<Map<String, String>>> result = new HashMap<>();

		String text = environment.getProperty("epom.endpoint.enun.text", "text");
		String value = environment.getProperty("epom.endpoint.enun.value", "value");

		ConcurrentHashMap<Class, Set<IEnum>> enumCache = EnumFactory.enumCache;
		Set<Entry<Class, Set<IEnum>>> entrySet = enumCache.entrySet();
		for (Entry<Class, Set<IEnum>> entry : entrySet) {
			String simpleName = entry.getKey().getSimpleName();
			Set<IEnum> enums = entry.getValue();
			List<Map<String, String>> datas = new ArrayList<Map<String, String>>();
			for (IEnum ienum : enums) {
				Map<String, String> data = new HashMap<>();
				data.put(text, ienum.getName());
				data.put(value, StrUtil.toString(ienum.getValue()));
				datas.add(data);
			}
			result.put(simpleName, datas);
		}

		return result;
	}

	/**
	 * 查询所有枚举信息
	 */
	@SuppressWarnings("rawtypes")
	@ApiOperation("查询枚举信息")
	@GetMapping("/web/enum/all")
	public Map<String, List<EnumData>> findEnums() {

		Map<String, List<EnumData>> result = new HashMap<>();

		ConcurrentHashMap<Class, Set<IEnum>> enumCache = EnumFactory.enumCache;
		Set<Entry<Class, Set<IEnum>>> entrySet = enumCache.entrySet();
		for (Entry<Class, Set<IEnum>> entry : entrySet) {
			String simpleName = entry.getKey().getSimpleName();
			Set<IEnum> enums = entry.getValue();
			List<EnumData> datas = new ArrayList<EnumData>();
			for (IEnum ienum : enums) {
				EnumData data = new EnumData();
				data.setText(ienum.getName());
				ienum.getValue();
				data.setValue(StrUtil.toString(ienum.getValue()));
				datas.add(data);
			}
			result.put(simpleName, datas);
		}

		return result;
	}

	/**
	 * 查询枚举信息
	 * 
	 */
	@ApiOperation("查询枚举信息")
	@GetMapping("/web/enum/type")
	public List<EnumData> findEnum(@RequestParam("key") String key) {
		return findEnums().get(key);
	}

	@Setter
	@Getter
	@ApiModel("枚举端点数据")
	class EnumData {
		@ApiModelProperty(value = "枚举名称", name = "text", example = "离职", required = false)
		private String text;
		
		@ApiModelProperty(value = "枚举值", name = "value", example = "0", required = false)
		private String value;
	}
}
